package com.douyu.water.core.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.douyu.water.core.PagePara;
import com.douyu.water.core.dao.MenuMapper;
import com.douyu.water.core.model.Menu;
import com.douyu.water.core.model.MenuExample;
import com.douyu.water.util.Func;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public List<Menu> select(Menu record, PagePara page) {
		List<Menu> list = null;
		MenuExample example = createExample(record,false);
		if(page != null)
		{
			int total = menuMapper.countByExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		example.setOrderByClause(" isort asc");
		list = menuMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insert(Menu record) {
		int flag = 0;
		if(record == null)
		{
			return flag;
		}
		if(checkSameName(record))
		{
			return 2;
		}
		String newid = Func.getNewId();
		record.setUcode(newid);
		record.setRcdtime(new Date());
		flag = menuMapper.insert(record);
		return flag;
	}

	@Override
	public int update(Menu record) {
		int flag = 0;
		if(record == null || StringUtils.isBlank(record.getUcode()))
		{
			return flag;
		}
		if(checkSameName(record))
		{
			return 2;
		}
		record.setRcdtime(new Date());
		flag = menuMapper.updateByPrimaryKey(record);
		return flag;
	}

	@Override
	public int delete(Menu record) {
		int flag = 0;
		if(record == null || StringUtils.isBlank(record.getUcode()))
		{
			return flag;
		}
		if(checkUse(record))
		{
			return 2;
		}
		flag = menuMapper.deleteByPrimaryKey(record.getUcode().trim());
		return flag;
	}

	@Override
	public Menu view(Menu record){
		Menu menu = null;
		if(record == null || StringUtils.isBlank(record.getUcode()))
		{
			return menu;
		}
		menu = menuMapper.selectByPrimaryKey(record.getUcode().trim());
		return menu;
	}
	/**
	 * 检查菜单是否正在被使用
	 * @param record Menu
	 * @return boolean
	 */
	private boolean checkUse(Menu record)
	{
		boolean flag = true;
		if(record == null || StringUtils.isBlank(record.getUcode()))
		{
			return flag;
		}
		int cnt = menuMapper.countByPrimary(record.getUcode().trim());
		if(cnt == 0)
		{
			flag = false;
		}
		return flag;
	}
	/**
	 * 检查菜单或操作名称是否重复
	 * @param record Menu
	 * @return boolean
	 */
	private boolean checkSameName(Menu record)
	{
		boolean flag = false;
		MenuExample example = createExample(record,true);
		int cnt = menuMapper.countByExample(example);
		if(cnt > 0)
		{
			flag = true;
		}
		return flag;
	}
	/**
	 * 根据菜单对象形成检索条件
	 * @param record Menu 
	 *        -菜单对象
	 * @return MenuExample
	 */
	private MenuExample createExample(Menu record, boolean equal)
	{
		MenuExample example = new MenuExample();
		MenuExample.Criteria criteria = example.createCriteria();
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getCategory()))
			{
				criteria.andCategoryEqualTo(record.getCategory().trim());
			}
			if(!StringUtils.isBlank(record.getUname()))
			{
				if(equal)
				{
					criteria.andUnameEqualTo(record.getUname().trim());
				}
				else{
					criteria.andUnameLike("%" + record.getUname().trim() + "%");	
				}				
			}
			if(!StringUtils.isBlank(record.getUtype()))
			{
				criteria.andUtypeEqualTo(record.getUtype().trim());
			}
			if(!StringUtils.isBlank(record.getUrl()))
			{
				criteria.andUrlLike("%" + record.getUrl().trim() + "%");
			}
			if(!StringUtils.isBlank(record.getParent()))
			{
				criteria.andParentEqualTo(record.getParent().trim());
			}
			if(!StringUtils.isBlank(record.getUcode()))
			{
				if(equal)
				{
					criteria.andUcodeNotEqualTo(record.getUcode().trim());
				}
				else
				{
					criteria.andUcodeEqualTo(record.getUcode().trim());
				}
			}
		}
		return example;
	}
}
